/* eslint-disable no-unused-vars */
/// <reference path="../typings/blockly.d.ts" />
/* eslint-disable no-undef */
// 给python 添加 字典类型 内置函数

// colour_blocks: { colourPrimary: '3e86ea' },
//     list_blocks: { colourPrimary: '123' },
//     logic_blocks: { colourPrimary: '3e86ea' },
//     loops_blocks: { colourPrimary: '3e86ea' },
//     math_blocks: { colourPrimary: '3e86ea' },
//     text_blocks: { colourPrimary: '3e86ea' },
//     variables_blocks: { colourPrimary: '3e86ea' },
//     variablesDynamic_blocks: { colourPrimary: '3e86ea' }

/**
 * @license
 * Copyright 2012 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @fileoverview Dict blocks for Blockly.
 *
 * This file is scraped to extract a .json file of block definitions. The array
 * passed to defineBlocksWithJsonArray(..) must be strict JSON: double quotes
 * only, no outside references, no functions, no trailing commas, etc. The one
 * exception is end-of-line comments, which the scraper will remove.
 * @author fraser@google.com (Neil Fraser)
 */
'use strict';

// goog.provide('Blockly.Blocks.dict'); // Deprecated
// goog.provide('Blockly.Constants.dict');

// goog.require('Blockly');
// goog.require('Blockly.Blocks');
// goog.require('Blockly.FieldDropdown');
// goog.require('Blockly.FieldLabel');
// goog.require('Blockly.Mutator');





/**
 * Unused constant for the common HSV hue for all blocks in this category.
 * @deprecated Use Blockly.Msg['LISTS_HUE']. (2018 April 5)
 */

Blockly.Constants.Dists = {}
Blockly.Constants.Dists.HUE = '165';

Blockly.defineBlocksWithJsonArray([{
  "type": "dict_create_empty",
  "message0": "创建空字典",
  "output": "Array",
  "colour": "a1a73d",
  "tooltip": "创建一个空的字典",

},
{
  "type": "dict_getIndex",
  "message0": "获取字典 %1 key为  %2 ",
  "inputsInline": true,
  "args0": [{
    "type": "input_value",
    "name": "ITEM",
    // "check": ["Object", "Array"]
  },
  {
    "type": "input_value",
    "name": "KEY"
  }
  ],
  "output": ["String", "Array", "Number", "Boolean"],
  "colour": "a1a73d",
  "tooltip": "你好",
},
{
  "type": "dict_setIndex",
  "message0": "设置字典 %1 key为 %2 值为 %3 ",
  "inputsInline": true,
  "args0": [{
    "type": "input_value",
    "name": "ITEM",
    // "check": ["Object", "Array"]
  },
  {
    "type": "input_value",
    "name": "KEY",
    // "check": ["Number", "String"]
  },
  {
    "type": "input_value",
    "name": "VALUE",
  }
  ],
  "colour": "a1a73d",
  "previousStatement": null,
  "nextStatement": null,
},
{
  "type": "dict_getkeys",
  "message0": "获取字典 %1 keys ",
  "inputsInline": true,
  "args0": [{
    "type": "input_value",
    "name": "ITEM",
    // "check": ["Object", "Array"]
  }],
  "output": ["Array"],
  "colour": "a1a73d",
  "tooltip": "你好",
}, {
  "type": "dict_getvalues",
  "message0": "获取字典 %1 values ",
  "inputsInline": true,
  "args0": [{
    "type": "input_value",
    "name": "ITEM",
  }],
  "output": ["Array"],
  "colour": "a1a73d",
  "tooltip": "你好",
},
])




Blockly.Python.dict = {}

Blockly.Python.dict_create_empty = function (a = this) {
  return [`{}`, Blockly.Python.ORDER_ATOMIC]
}

Blockly.Python.dict_getIndex = function (a = this) {
  const item = Blockly.Python.valueToCode(a, 'ITEM', Blockly.Python.ORDER_NONE)
  const key = Blockly.Python.valueToCode(a, 'KEY', Blockly.Python.ORDER_MEMBER)

  return [`${item}[${key}]`, Blockly.Python.ORDER_ATOMIC]
}

Blockly.Python.dict_setIndex = function (a = this) {

  const item = Blockly.Python.valueToCode(a, 'ITEM', Blockly.Python.ORDER_NONE)
  const key = Blockly.Python.valueToCode(a, 'KEY', Blockly.Python.ORDER_MEMBER)
  const value = Blockly.Python.valueToCode(a, 'VALUE', Blockly.Python.ORDER_NONE)
  return `${item}[${key}] = ${value}\n`
}

Blockly.Python.dict_getkeys = function (a = this) {
  const item = Blockly.Python.valueToCode(a, 'ITEM', Blockly.Python.ORDER_NONE)

  return [`${item}.keys()`, Blockly.Python.ORDER_ATOMIC]
}

Blockly.Python.dict_getvalues = function (a = this) {
  const item = Blockly.Python.valueToCode(a, 'ITEM', Blockly.Python.ORDER_NONE)

  return [`${item}.values()`, Blockly.Python.ORDER_ATOMIC]
}

Blockly.defineBlocksWithJsonArray([{
  "type": "func_print",
  "message0": "print %1",
  "args0": [{
    "type": "input_value",
    "name": "ITEM",
  }],
  "previousStatement": null,
  "colour": "6b80ff",
  "nextStatement": null,
},
{
  "type": "func_sleep",
  "message0": "sleep %1",
  "args0": [{
    "type": "input_value",
    "name": "ITEM",
  }],
  "previousStatement": null,
  "colour": "6b80ff",
  "nextStatement": null,
},
{
  "type": "func_list",
  "message0": "list %1",
  "args0": [{
    "type": "input_value",
    "name": "ITEM",
  }],
  "output": ["Object", "Array"],
  "colour": "6b80ff",
  "tooltip": "你好",
},
{
  "type": "func_len",
  "message0": "len %1",
  "args0": [{
    "type": "input_value",
    "name": "ITEM",
  }],
  "output": ["String", "Number"],
  "colour": "6b80ff",
  "tooltip": "你好",
},
{
  "type": "func_str",
  "message0": "str %1",
  "args0": [{
    "type": "input_value",
    "name": "ITEM",
  }],
  "output": ["String"],
  "colour": "6b80ff",
  "tooltip": "你好",
},
{
  "type": "func_in",
  "inputsInline": true,
  "message0": "%1 in %2",
  "args0": [{
    "type": "input_value",
    "name": "ITEM",
  },
  {
    "type": "input_value",
    "name": "ITEM2",
  }
  ],
  "output": "Boolean",
  "colour": "6b80ff",
  "tooltip": "in",
},
])
Blockly.Blocks.factory = {}
Blockly.Blocks.factory.HUE = '125'
Blockly.Blocks['factory_import'] = {
  init: function () {
    // this.setColour(Blockly.Blocks.factory.HUE);
    this.setColour("6b80ff")
    this.appendDummyInput("")
      .appendField("import ")
      .appendField(new Blockly.FieldTextInput('Test'), 'IMPORT')
    this.setPreviousStatement(true);
    this.setNextStatement(true);
  }
};

Blockly.Blocks['factory_block'] = {
  init: function () {
    this.setColour("6b80ff")
    this.appendDummyInput()
      .appendField(new Blockly.FieldTextInput(''), 'VALUE')
    this.setPreviousStatement(true);
    this.setNextStatement(true);
  },
}

Blockly.Blocks['factory_block_return'] = {
  init: function () {
    this.setColour("6b80ff")
    this.appendDummyInput("")
      .appendField(new Blockly.FieldTextInput('test'), 'VALUE');
    this.setOutput(true);
  }
};


Blockly.Python.func = {}

Blockly.Python.func_print = function (a = this) {
  const item = Blockly.Python.valueToCode(a, 'ITEM', Blockly.Python.ORDER_NONE)
  return `print(${item})\n`
}

Blockly.Python.func_in = function (a = this) {
  const item = Blockly.Python.valueToCode(a, 'ITEM', Blockly.Python.ORDER_NONE)
  const item2 = Blockly.Python.valueToCode(a, 'ITEM2', Blockly.Python.ORDER_NONE)
  return [`${item} in ${item2}`, Blockly.Python.ORDER_ATOMIC]
}

Blockly.Python.func_sleep = function (a = this) {
  Blockly.Python.definitions_['import_time'] = `import time`;

  const item = Blockly.Python.valueToCode(a, 'ITEM', Blockly.Python.ORDER_NONE)
  return `time.sleep(${item})\n`
}

Blockly.Python.func_list = function (a = this) {
  const item = Blockly.Python.valueToCode(a, 'ITEM', Blockly.Python.ORDER_NONE)

  return [`list(${item})`, Blockly.Python.ORDER_ATOMIC]
}
Blockly.Python.func_len = function (a = this) {
  const item = Blockly.Python.valueToCode(a, 'ITEM', Blockly.Python.ORDER_NONE)

  return [`len(${item})`, Blockly.Python.ORDER_ATOMIC]
}
Blockly.Python.func_str = function (a = this) {
  const item = Blockly.Python.valueToCode(a, 'ITEM', Blockly.Python.ORDER_NONE)
  return [`str(${item})`, Blockly.Python.ORDER_ATOMIC]
}


Blockly.Python.factory_import = function () {
  var IMPORT = this.getFieldValue('IMPORT');
  Blockly.Python.definitions_['import_' + IMPORT] = `import ${IMPORT}`;
  return '';
};

Blockly.Python['factory_block'] = function (a = this) {
  var VALUE = a.getFieldValue('VALUE');
  return VALUE + '\n';
}

Blockly.Python['factory_block_return'] = function (a = this) {
  var VALUE = a.getFieldValue('VALUE');
  return [VALUE, Blockly.Python.ORDER_ATOMIC];
}


// head 
Blockly.defineBlocksWithJsonArray([{
  "type": "head_Serial_Init",
  "message0": "串口初始化",
  "previousStatement": null,
  "nextStatement": null,
  "colour": "a00000"

}, {
  "type": "head_Baidu_Gesture_Init",
  "message0": "手势api初始化 app_id %1 api_key %2 secret_key %3",
  "args0": [{
    "type": "input_value",
    "name": "A0",
    "check": "String",
    "align": "RIGHT"
  },
  {
    "type": "input_value",
    "name": "A1",
    "check": "String",
    "align": "RIGHT"
  },
  {
    "type": "input_value",
    "name": "A2",
    "check": "String",
    "align": "RIGHT"
  }
  ],
  "previousStatement": null,
  "nextStatement": null,
  "colour": "a00000"
},
{
  "type": "head_Baidu_Audio_Init",
  "message0": "语音api初始化 app_id %1 api_key %2 secret_key %3",
  "args0": [{
    "type": "input_value",
    "name": "A0",
    "check": "String",
    "align": "RIGHT"
  },
  {
    "type": "input_value",
    "name": "A1",
    "check": "String",
    "align": "RIGHT"
  },
  {
    "type": "input_value",
    "name": "A2",
    "check": "String",
    "align": "RIGHT"
  }
  ],
  "previousStatement": null,
  "nextStatement": null,
  "colour": "a00000"
},
{
  "type": "head_Camera_Init",
  "message0": "摄像头参数初始化",
  "previousStatement": null,
  "nextStatement": null,
  "colour": "a00000"
},
{
  "type": "head_Camera_Set",
  "message0": "摄像头参数设置 width %1 height %2 brightness %3 saturation %4 Hue %5",
  "args0": [{
    "type": "input_value",
    "name": "A0",
    "check": "Number",
    "align": "RIGHT"
  },
  {
    "type": "input_value",
    "name": "A1",
    "check": "Number",
    "align": "RIGHT"
  },
  {
    "type": "input_value",
    "name": "A2",
    "check": "Number",
    "align": "RIGHT"
  },
  {
    "type": "input_value",
    "name": "A3",
    "check": "Number",
    "align": "RIGHT"
  },
  {
    "type": "input_value",
    "name": "A4",
    "check": "Number",
    "align": "RIGHT"
  }
  ],
  "previousStatement": null,
  "nextStatement": null,
  "colour": "a00000"
},
{
  "type": "head_Window_Init",
  "message0": "摄像头窗口初始化 window_name %1 window_width %2 window_height %3",
  "args0": [{
    "type": "input_value",
    "name": "A0",
    "check": "String",
    "align": "RIGHT"
  },
  {
    "type": "input_value",
    "name": "A1",
    "check": "Number",
    "align": "RIGHT"
  },
  {
    "type": "input_value",
    "name": "A2",
    "check": "Number",
    "align": "RIGHT"
  },
  ],
  "previousStatement": null,
  "nextStatement": null,
  "colour": "a00000"
},
{
  "type": "head_Show_Window",
  "message0": "显示摄像头静态图像 window_name %1",
  "args0": [{
    "type": "input_value",
    "name": "A0",
    "check": "String",
    "align": "RIGHT"
  }],
  "previousStatement": null,
  "nextStatement": null,
  "colour": "a00000"
},
{
  "type": "head_Dynamic_Show",
  "message0": "显示摄像头动态图像",
  "output": "Function",
  "colour": "a00000"
},
{
  "type": "head_Threading",
  "message0": "多线程动态图像 def_name %1",
  "args0": [{
    "type": "input_value",
    "name": "A0",
    "check": "Function",
    "align": "RIGHT"
  }],
  "previousStatement": null,
  "nextStatement": null,
  "colour": "a00000"
},
{
  "type": "head_Pic_Analysis",
  "message0": "图像分析",
  "output": null,
  "colour": "a00000"
},
{
  "type": "head_get_file_content",
  "message0": "读取音频文件 %1",
  "args0": [{
    "type": "input_value",
    "name": "A0",
    "check": "String",
    "align": "RIGHT"
  }],
  "output": null,
  "colour": "a00000"
},
{
  "type": "head_Record",
  "message0": "录制音频文件 录制时间(秒) %1  保存地址 %2",
  "args0": [{
    "type": "input_value",
    "name": "A0",
    "check": "Number",
    "align": "RIGHT"
  },
  {
    "type": "input_value",
    "name": "A1",
    "check": "String",
    "align": "RIGHT"
  }
  ],
  "previousStatement": null,
  "nextStatement": null,
  "colour": "a00000"
},
{
  "type": "head_stt",
  "message0": "识别音频文件  %1",
  "args0": [{
    "type": "input_value",
    "name": "A0",
    "check": "String",
    "align": "RIGHT"
  }],
  "output": null,
  "colour": "a00000"
},
{
  "type": "head_Fingers_Degree_Set",
  "message0": "设置手的角度 大拇指%1 食指%2 中指%3 无名指%4 小拇指%5 手掌%6",
  "args0": [{
    "type": "input_value",
    "name": "A0",
    "check": "Number",
    "align": "RIGHT"
  }, {
    "type": "input_value",
    "name": "A1",
    "check": "Number",
    "align": "RIGHT"
  }, {
    "type": "input_value",
    "name": "A2",
    "check": "Number",
    "align": "RIGHT"
  }, {
    "type": "input_value",
    "name": "A3",
    "check": "Number",
    "align": "RIGHT"
  }, {
    "type": "input_value",
    "name": "A4",
    "check": "Number",
    "align": "RIGHT"
  }, {
    "type": "input_value",
    "name": "A5",
    "check": "Number",
    "align": "RIGHT"
  }],
  "previousStatement": null,
  "nextStatement": null,
  "tooltip": "你好",
  "colour": "a00000"
},
])


const head = {
  Serial_Init: `serial_open_flag = 0
serialFd = 0

def Serial_Init(baudrate = 9600):
    global serial_open_flag
    global serialFd
    plist = list(serial.tools.list_ports.comports())
    if len(plist) <= 0:
        print ("无法找到串口!")
    else:
        if serial_open_flag == 0:
            plist_0 =list(plist[0])
            print(plist[0])
            serialName = plist_0[0]
            serialFd = serial.Serial(serialName,baudrate,timeout = 60)
            print("串口打开成功!波特率为",baudrate)
            serial_open_flag = 1`,
  Baidu_Gesture_Init: `baidu_gesture_flag = 0
gesture_client = 0

def Baidu_Gesture_Init(app_id='20335137',
			api_key='aoBoiOzHT9oQIREXyjo4m1jg',
			secret_key='rudq6Fch2e3vCmPxLLPsVPAWYrmD5zgq'):
                global gesture_client
                global baidu_gesture_flag
                if baidu_gesture_flag == 0:
                    APP_ID = app_id    #api的ID，KEY，SECRET_KEY信息
                    API_KEY = api_key
                    SECRET_KEY = secret_key
                    gesture_client = AipBodyAnalysis(APP_ID,API_KEY,SECRET_KEY) #初始化client
                    baidu_gesture_flag = 1     `,
  Baidu_Audio_Init: `audio_client = 0
baidu_audio_flag = 0

def Baidu_Audio_Init(app_id='20335137',
	api_key='aoBoiOzHT9oQIREXyjo4m1jg',
	secret_key='rudq6Fch2e3vCmPxLLPsVPAWYrmD5zgq'):
        global audio_client
        global baidu_audio_flag
        if baidu_audio_flag == 0:
            APP_ID = app_id    #api的ID，KEY，SECRET_KEY信息
            API_KEY = api_key
            SECRET_KEY = secret_key
            audio_client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) #初始化client
            baidu_audio_flag = 1
	`,
  Camera_Init: `video_flag = 0
VC = 0
frame = None

def Camera_Init():
    global video_flag
    global VC
    global frame
    if video_flag == 0:
        VC = cv2.VideoCapture(0)
        VC.set(6,cv2.VideoWriter.fourcc('M','J','P','G'))
        VC.set(3,320)
        VC.set(4,240)
        VC.set(10,0.5)
        VC.set(11,0.5)
        VC.set(12,0.5)
        if VC.isOpened():
            rval,frame = VC.read()
            if rval == True:
                print("摄像头初始化成功!") 
                video_flag = 1   
        else:
            print("摄像头初始化失败!")
            video_flag = 0
	`,
  Camera_Set: `camera_set_flag = 0

def Camera_Set(width=320,height=240,brightness=0.5,saturation=0.5,Hue=0.5):
    global VC
    global camera_set_flag
    if camera_set_flag == 0:
        VC.set(3,width)#分辨率宽度值
        VC.set(4,height)#分辨率高度值
        VC.set(10,brightness)#图像亮度
        VC.set(11,saturation)#饱和度
        VC.set(12,Hue)#色相
        camera_set_flag = 1
	`,
  Window_Init: `window_init_flag = 0

def Window_Init(window_name = "cap",window_width=640,window_height=480):
    global window_init_flag
    global frame
    if window_init_flag == 0:
        cv2.namedWindow(window_name,0);
        cv2.resizeWindow(window_name, window_width, window_height);
        cv2.imshow(window_name,frame)
        window_init_flag = 1
	`,
  Show_Window: `window_init_flag = 0

def Show_Window(window_name="cap",window_frame=frame):
    global frame
    global VC
    rval,frame = VC.read()
    cv2.imshow(window_name,frame)
    cv2.waitKey(10)
	`,
  Dynamic_Show: `def Dynamic_Show(window_name="cap",window_frame=frame):
    global frame
    global VC
    while 1:
        rval,frame = VC.read()
        cv2.imshow(window_name,frame)
        cv2.waitKey(10) 
	`,
  Threading: `def Threading(function=Dynamic_Show):
    global T1
    T1 = threading.Thread(target=function)
	T1.start()`,
  Pic_Analysis: `def Pic_Analysis():
    global frame
    global VC
    global gesture_client
    rval,frame = VC.read()
    if rval:
        retval, buffer = cv2.imencode('.jpg', frame)
        jpg_as_text = base64.b64encode(buffer)
        jpg_as_np = np.frombuffer(buffer, dtype=np.uint8)
        return gesture_client.gesture(jpg_as_np)  
	`,
  get_file_content: `def get_file_content(filePath = audiopath):   
    with open(filePath, 'rb') as fp:
		    return fp.read()`,
  Record: `def Record(RECORD_SECONDS=2, path = audiopath):
    p = pyaudio.PyAudio()
    stream = p.open(format=pyaudio.paInt16, 
                channels=1, 
                rate=16000, 
                input=True, 
                frames_per_buffer=1024) 
    print("共"+str(RECORD_SECONDS)+"秒录音")
    print("开始录音,请说话......") 
        
    frames = [] 
        
    for _i in range(0, int(16000 / 1024 * RECORD_SECONDS)):
        data = stream.read(1024) 
        frames.append(data)
        #print(i,"秒过去")
        
    print("录音结束!") 
        
    stream.stop_stream() 
    stream.close() 
    p.terminate() 
        
    wf = wave.open(path, 'wb') 
    wf.setnchannels(1) 
    wf.setsampwidth(p.get_sample_size(pyaudio.paInt16)) 
    wf.setframerate(16000) 
    wf.writeframes(b''.join(frames)) 
    wf.close()
		`,
  stt: `def stt(file):
  global serialFd
  global audio_client
  global result
  result = audio_client.asr(file, 'wav', 16000, {'dev_pid': 1537})
  return result`,
  Fingers_Degree_Set: `def Fingers_Degree_Set(thumb=99,index=99,middle=99,ring=99,little=99,bottom=50):
    global serialFd
    Gap = ','
    serialFd.write(bytes(str(thumb)+Gap+
                         str(index)+Gap+
                         str(middle)+Gap+
                         str(ring)+Gap+
                         str(little)+Gap+
                         str(bottom)+Gap+
                         'z','utf-8'))
`
}

Blockly.Python.head_Serial_Init = function (a = this) {
  // 导入包
  Blockly.Python.definitions_['import_serial'] = `import serial`;
  Blockly.Python.definitions_['import_serial.tools.list_ports'] = `import serial.tools.list_ports`;
  // 创建变量
  this.workspace.createVariable('serial_open_flag')
  this.workspace.createVariable('serialFd')
  // 声明函数
  Blockly.Python.definitions_['%Serial_Init'] = head.Serial_Init
  const code = `Serial_Init()\n`
  return code
}

Blockly.Python.head_Baidu_Gesture_Init = function (a = this) {
  // 引入包
  Blockly.Python.definitions_['import_AipBodyAnalysis'] = `from aip import AipBodyAnalysis`;
  Blockly.Python.definitions_['import_os'] = `import os`;
  // 声明变量
  this.workspace.createVariable('baidu_gesture_flag')
  // 声明函数
  Blockly.Python.definitions_['%Baidu_Gesture_Init'] = head.Baidu_Gesture_Init
  // 设置参数
  const A0 = Blockly.Python.valueToCode(a, 'A0', Blockly.Python.ORDER_NONE)
  const A1 = Blockly.Python.valueToCode(a, 'A1', Blockly.Python.ORDER_NONE)
  const A2 = Blockly.Python.valueToCode(a, 'A2', Blockly.Python.ORDER_NONE)

  const code = `Baidu_Gesture_Init(${A0 || '20335137'}, ${A1 || 'aoBoiOzHT9oQIREXyjo4m1jg'}, ${A2 || 'rudq6Fch2e3vCmPxLLPsVPAWYrmD5zgq'})\n`
  return code
}

Blockly.Python.head_Baidu_Audio_Init = function (a = this) {
  // 导入包
  Blockly.Python.definitions_['import_AipSpeech'] = `from aip import AipSpeech`;
  // 创建变量
  this.workspace.createVariable('audio_client')
  this.workspace.createVariable('baidu_audio_flag')
  // 声明函数
  Blockly.Python.definitions_['%Baidu_Audio_Init'] = head.Baidu_Audio_Init

  const A0 = Blockly.Python.valueToCode(a, 'A0', Blockly.Python.ORDER_NONE)
  const A1 = Blockly.Python.valueToCode(a, 'A1', Blockly.Python.ORDER_NONE)
  const A2 = Blockly.Python.valueToCode(a, 'A2', Blockly.Python.ORDER_NONE)

  return `Baidu_Audio_Init(${A0 || '20335137'}, ${A1 || 'aoBoiOzHT9oQIREXyjo4m1jg'}, ${A2 || 'rudq6Fch2e3vCmPxLLPsVPAWYrmD5zgq'})\n`
}

Blockly.Python.head_Camera_Init = function (a = this) {
  Blockly.Python.definitions_['import_cv2'] = `import cv2`;
  Blockly.Python.definitions_['import_numpy'] = `import numpy as np`;
  // 声明变量
  this.workspace.createVariable('video_flag')
  // 声明函数
  Blockly.Python.definitions_['%Camera_Init'] = head.Camera_Init

  const code = `Camera_Init()\n`
  return code
}

Blockly.Python.head_Camera_Set = function (a = this) {
  Blockly.Python.definitions_['import_cv2'] = `import cv2`;
  // 声明变量
  this.workspace.createVariable('camera_set_flag')
  // 声明方法
  Blockly.Python.definitions_['%Camera_Set'] = head.Camera_Set
  // 查找参数
  const A0 = Blockly.Python.valueToCode(a, 'A0', Blockly.Python.ORDER_NONE) || 320
  const A1 = Blockly.Python.valueToCode(a, 'A1', Blockly.Python.ORDER_NONE) || 240
  const A2 = Blockly.Python.valueToCode(a, 'A2', Blockly.Python.ORDER_NONE) || 0.5
  const A3 = Blockly.Python.valueToCode(a, 'A3', Blockly.Python.ORDER_NONE) || 0.5
  const A4 = Blockly.Python.valueToCode(a, 'A4', Blockly.Python.ORDER_NONE) || 0.5

  return `Camera_Set(${A0},${A1},${A2},${A3},${A4})\n`

}
Blockly.Python.head_Window_Init = function (a = this) {
  Blockly.Python.definitions_['import_cv2'] = `import cv2`;
  // 声明变量
  this.workspace.createVariable('window_init_flag')
  // 声明方法
  Blockly.Python.definitions_['%Window_Init'] = head.Window_Init
  // 查找参数
  const A0 = Blockly.Python.valueToCode(a, 'A0', Blockly.Python.ORDER_NONE) || '"cap"'
  const A1 = Blockly.Python.valueToCode(a, 'A1', Blockly.Python.ORDER_NONE) || 640
  const A2 = Blockly.Python.valueToCode(a, 'A2', Blockly.Python.ORDER_NONE) || 480

  return `Window_Init(${A0},${A1},${A2})\n`

}
Blockly.Python.head_Show_Window = function (a = this) {
  // 引入包
  Blockly.Python.definitions_['import_cv2'] = `import cv2`;
  // 创建变量
  this.workspace.createVariable('window_init_flag')
  // 创建函数

  Blockly.Python.definitions_['%Show_Window'] = head.Show_Window

  const A0 = Blockly.Python.valueToCode(a, 'A0', Blockly.Python.ORDER_NONE) || 320
  const A1 = Blockly.Python.valueToCode(a, 'A1', Blockly.Python.ORDER_NONE) || 320
  return `Show_Window(${A0}, ${A1})\n`
}

Blockly.Python.head_Dynamic_Show = function (a = this) {
  // 引入包
  Blockly.Python.definitions_['import_cv2'] = `import cv2`;
  Blockly.Python.definitions_['%Dynamic_Show'] = head.Dynamic_Show
  return ['Dynamic_Show', 0]
}

Blockly.Python.head_Threading = function (a = this) {
  Blockly.Python.definitions_['import_threading'] = `import threading`;
  const A0 = Blockly.Python.valueToCode(a, 'A0', Blockly.Python.ORDER_NONE)
  const code = `threading.Thread(target = ${A0}).start()\n`
  return code
}

Blockly.Python.head_Pic_Analysis = function (a = this) {
  // 导入包
  Blockly.Python.definitions_['import_cv2'] = `import cv2`;
  Blockly.Python.definitions_['import_numpy'] = `import numpy as np`;
  Blockly.Python.definitions_['import_base64'] = `import base64`;

  // 声明函数
  Blockly.Python.definitions_['%Pic_Analysis'] = head.Pic_Analysis

  const code = `Pic_Analysis()`
  return [code, Blockly.Python.ORDER_ATOMIC]
}

// 读取音频文件
Blockly.Python.head_get_file_content = function (a = this) {
  Blockly.Python.definitions_['import_os'] = `import os`
  Blockly.Python.definitions_['variable_audiopath'] = `audiopath = os.path.join(os.path.expanduser("~"), 'Desktop', 'HTE_voice.wav')`

  Blockly.Python.definitions_['%get_file_content'] = head.get_file_content

  const A0 = Blockly.Python.valueToCode(a, 'A0', Blockly.Python.ORDER_NONE) || `audiopath`
  const code = `get_file_content(${A0})`
  return [code, Blockly.Python.ORDER_ATOMIC]
}

// 录制音频文件
Blockly.Python.head_Record = function (a = this) {
  Blockly.Python.definitions_['import_os'] = `import os`;
  Blockly.Python.definitions_['import_pyaudio'] = `import pyaudio`;
  Blockly.Python.definitions_['import_wave'] = `import wave`;
  Blockly.Python.definitions_['variable_audiopath'] = `audiopath = os.path.join(os.path.expanduser("~"), 'Desktop', 'HTE_voice.wav')`
  // 声明变量
  this.workspace.createVariable('p')
  // 声明函数
  Blockly.Python.definitions_['%Record'] = head.Record
  const A0 = Blockly.Python.valueToCode(a, 'A0', Blockly.Python.ORDER_NONE) || 2
  const A1 = Blockly.Python.valueToCode(a, 'A1', Blockly.Python.ORDER_NONE) || 'audiopath'

  return `Record(${A0}, ${A1})\n`
}

// 识别音频文件
Blockly.Python.head_stt = function (a = this) {
  Blockly.Python.definitions_['import_os'] = `import os`;
  Blockly.Python.definitions_['import_AipSpeech'] = `from aip import AipSpeech`;
  Blockly.Python.definitions_['variable_audiopath'] = `audiopath = os.path.join(os.path.expanduser("~"), 'Desktop', 'HTE_voice.wav')`

  // 变量
  this.workspace.createVariable('client')
  this.workspace.createVariable('result')
  // 声明函数
  Blockly.Python.definitions_['%stt'] = head.stt
  const A0 = Blockly.Python.valueToCode(a, 'A0', Blockly.Python.ORDER_NONE) || `audiopath`
  return [`stt(${A0})`, Blockly.Python.ORDER_ATOMIC]
}

Blockly.Python.head_Fingers_Degree_Set = function (a = this) {
  Blockly.Python.definitions_['import_serial'] = 'import serial'
  Blockly.Python.definitions_['%Fingers_Degree_Set'] = head.Fingers_Degree_Set
  const A0 = Blockly.Python.valueToCode(a, 'A0', Blockly.Python.ORDER_NONE) || 2
  const A1 = Blockly.Python.valueToCode(a, 'A1', Blockly.Python.ORDER_NONE) || 2
  const A2 = Blockly.Python.valueToCode(a, 'A2', Blockly.Python.ORDER_NONE) || 2
  const A3 = Blockly.Python.valueToCode(a, 'A3', Blockly.Python.ORDER_NONE) || 2
  const A4 = Blockly.Python.valueToCode(a, 'A4', Blockly.Python.ORDER_NONE) || 2
  const A5 = Blockly.Python.valueToCode(a, 'A5', Blockly.Python.ORDER_NONE) || 2

  return `Fingers_Degree_Set(${A0}, ${A1}, ${A2},${A3}, ${A4}, ${A5})\n`
}